//
//  PKReadDetailViewController.m
//  PianKe
//
//  Created by ybq on 15/12/29.
//  Copyright © 2015年 B7_Young. All rights reserved.
//

#import "PKReadDetailViewController.h"
#import "PKReadNewTableViewController.h"
#import "PKReadHotTableViewController.h"
#import "PKReadDetailModel.h"


@interface PKReadDetailViewController () <UIScrollViewDelegate>

@property (nonatomic, strong) UIScrollView *mainScrollView;
@property (nonatomic, strong) PKReadNewTableViewController *readNewTVC;
@property (nonatomic, strong) PKReadHotTableViewController *readHotTVC;
@property (nonatomic, strong) PKReadDetailModel *modelNew;
@property (nonatomic, strong) PKReadDetailModel *modelHot;
//@property (nonatomic, strong) NSMutableArray *listNewArray;
//@property (nonatomic, strong) NSMutableArray *listHotArray;
@property (nonatomic, strong) UIImageView *shortline;
@property (nonatomic, strong) UIButton *newlyButton;
@property (nonatomic, strong) UIButton *hotButton;
@property (nonatomic, assign) NSInteger newRequestStart;
@property (nonatomic, assign) NSInteger hotRequestStart;


@end

@implementation PKReadDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor blackColor];
    
    _newRequestStart = 0;
    [self creatUI];
    [self creatVC];
    [self requestNewData];
    [self requestHotData];
    
}

- (void)requestNewData {
    NSMutableDictionary *parDic = [NSMutableDictionary dictionary];
    parDic[@"auth"] = @"Wc06FCrkoq1DCMVzGMTikDJxQ8bm3Mrm2NpT9qWjwzcWP23tBKQx1c4P0";
    parDic[@"client"] = @"1";
    parDic[@"deviceid"] = @"07EBFC18-9BF8-47D1-9FEA-79BBF44D4C9E";
    parDic[@"version"] = @"3.0.6";
    parDic[@"typeid"] = _typeId;
    parDic[@"sort"] = @"addtime";
    parDic[@"limit"] = @"10";
    parDic[@"start"] = [NSString stringWithFormat:@"%ld",_newRequestStart];
    
    [PKRequestManager requestWithUrlString:@"http://api2.pianke.me/read/columns_detail" parDic:parDic Method:POST finish:^(NSData *data) {
        NSDictionary *dataDic = [NSJSONSerialization JSONObjectWithData:data options:(NSJSONReadingMutableContainers) error:nil];
        NSDictionary *infoDic = dataDic[@"data"];
        
        if (_newRequestStart == 0) {
            [_readNewTVC.listArray removeAllObjects];
        }

        _modelNew = [[PKReadDetailModel alloc] init];
        [_modelNew setValuesForKeysWithDictionary:infoDic[@"columnsInfo"]];
        for (NSDictionary *dic in infoDic[@"list"]) {
            PKReadDetailModel *model = [[PKReadDetailModel alloc] init];
            [model setValuesForKeysWithDictionary:dic];
            [_readNewTVC.listArray addObject:model];
        }
            [_readNewTVC.tableView.mj_header endRefreshing];
            [_readNewTVC.tableView.mj_footer endRefreshing];
            [_readNewTVC.tableView reloadData];
        } error:^(NSError *error) {
        
    }];
}

- (void)requestHotData {
    NSMutableDictionary *parDic = [NSMutableDictionary dictionary];
    parDic[@"auth"] = @"Wc06FCrkoq1DCMVzGMTikDJxQ8bm3Mrm2NpT9qWjwzcWP23tBKQx1c4P0";
    parDic[@"client"] = @"1";
    parDic[@"deviceid"] = @"07EBFC18-9BF8-47D1-9FEA-79BBF44D4C9E";
    parDic[@"version"] = @"3.0.6";
    parDic[@"typeid"] = _typeId;
    parDic[@"sort"] = @"hot";;
    parDic[@"limit"] = @"10";
    parDic[@"start"] = [NSString stringWithFormat:@"%ld",_hotRequestStart];
    
    [PKRequestManager requestWithUrlString:@"http://api2.pianke.me/read/columns_detail" parDic:parDic Method:POST finish:^(NSData *data) {
        NSDictionary *dataDic = [NSJSONSerialization JSONObjectWithData:data options:(NSJSONReadingMutableContainers) error:nil];
        NSDictionary *infoDic = dataDic[@"data"];
        
        if (_hotRequestStart == 0) {
            [_readHotTVC.listArray removeAllObjects];
        }
        
        _modelHot = [[PKReadDetailModel alloc] init];
        [_modelHot setValuesForKeysWithDictionary:infoDic[@"columnsInfo"]];
        for (NSDictionary *dic in infoDic[@"list"]) {
            PKReadDetailModel *model = [[PKReadDetailModel alloc] init];
            [model setValuesForKeysWithDictionary:dic];
            [_readHotTVC.listArray addObject:model];
        }
        [_readHotTVC.tableView.mj_header endRefreshing];
        [_readHotTVC.tableView.mj_footer endRefreshing];
        [_readHotTVC.tableView reloadData];
        
    } error:^(NSError *error) {
        
    }];
}


-(void)creatUI {
    
    PKNavigationView *navView = [[PKNavigationView alloc] initWithFrame:CGRectMake(0, 20, kWidth, 45)];
    [navView.leftButton setImage:[UIImage imageNamed:@"back"] forState:(UIControlStateNormal)];
    navView.title.text = _typeName;
    [navView.leftButton addTarget:self action:@selector(backAction:) forControlEvents:(UIControlEventTouchUpInside)];
    
    _newlyButton = [UIButton buttonWithType:(UIButtonTypeCustom)];
    _newlyButton.frame = CGRectMake(250, 10, 19, 19);
    [_newlyButton setImage:[UIImage imageNamed:@"xin"] forState:(UIControlStateNormal)];
    [_newlyButton addTarget:self action:@selector(buttonAction:) forControlEvents:(UIControlEventTouchUpInside)];
    [navView addSubview:_newlyButton];
    
    _hotButton = [UIButton buttonWithType:(UIButtonTypeCustom)];
    _hotButton.frame = CGRectMake(325, 10, 20, 20);
    [_hotButton setImage:[UIImage imageNamed:@"hot"] forState:(UIControlStateNormal)];
    [_hotButton addTarget:self action:@selector(buttonAction:) forControlEvents:(UIControlEventTouchUpInside)];
    _hotButton.alpha = 0.3;
    [navView addSubview:_hotButton];
    
    _shortline = [[UIImageView alloc] initWithFrame:CGRectMake(255, 43, 10, 1)];
    _shortline.backgroundColor = [UIColor blackColor];
    [navView addSubview:_shortline];
    
    [self.view addSubview:navView];
    
    // 关闭scrollView自动布局
    self.automaticallyAdjustsScrollViewInsets = NO;
    _mainScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 64, kWidth, kHeight - 64)];
    _mainScrollView.contentSize = CGSizeMake(kWidth * 2, kHeight - 64);
    _mainScrollView.bounces = NO;
    _mainScrollView.delegate = self;
    _mainScrollView.pagingEnabled = YES;
    _mainScrollView.showsVerticalScrollIndicator = NO;
    _mainScrollView.showsHorizontalScrollIndicator = NO;
    _mainScrollView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:_mainScrollView];
}

- (void)buttonAction:(UIButton *)sender {
    if ([sender isEqual:_newlyButton]) {
        if (_mainScrollView.contentOffset.x != 0) {
            [_mainScrollView setContentOffset:CGPointMake(0, 0) animated:YES];
            CGRect frame = _shortline.frame;
            frame.origin.x = 255;
            [UIView animateWithDuration:.2 animations:^{
                _shortline.frame = frame;
            }];
            _newlyButton.alpha = 1;
            _hotButton.alpha = .3;
        }
    } else {
        if (_mainScrollView.contentOffset.x != kWidth) {
            [_mainScrollView setContentOffset:CGPointMake(kWidth, 0) animated:YES];
            CGRect frame = _shortline.frame;
            frame.origin.x = 330;
            [UIView animateWithDuration:.2 animations:^{
                _shortline.frame = frame;
            }];
            _newlyButton.alpha = .3;
            _hotButton.alpha = 1;

        }
    }
}

- (void)backAction:(UIButton *)sender {
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)creatVC {
    
    _readNewTVC = [[PKReadNewTableViewController alloc] init];
    _readNewTVC.tableView.frame = CGRectMake(0, 0, kWidth, _mainScrollView.frame.size.height);
    _readNewTVC.listArray = [NSMutableArray array];
    _readNewTVC.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        _newRequestStart = 0;
        [self requestNewData];
    }];
    
    _readNewTVC.tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        _newRequestStart += 10;
        [self requestNewData];
    }];
    
    [self addChildViewController:_readNewTVC];
    [_mainScrollView addSubview:_readNewTVC.tableView];
    
    _readHotTVC = [[PKReadHotTableViewController alloc] init];
    _readHotTVC.tableView.frame = CGRectMake(kWidth, 0, kWidth, _mainScrollView.frame.size.height);
    _readHotTVC.listArray = [NSMutableArray array];
    _readHotTVC.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        _hotRequestStart = 0;
        [self requestHotData];
    }];
    
    _readHotTVC.tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        _hotRequestStart += 10;
        [self requestHotData];
    }];
    
    [self addChildViewController:_readHotTVC];
    [_mainScrollView addSubview:_readHotTVC.tableView];
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    if (scrollView.contentOffset.x == kWidth) {
        CGRect frame = _shortline.frame;
        frame.origin.x = 330;
        [UIView animateWithDuration:.2 animations:^{
            _shortline.frame = frame;
        }];
        _newlyButton.alpha = .3;
        _hotButton.alpha = 1;
    } else {
        CGRect frame = _shortline.frame;
        frame.origin.x = 255;
        [UIView animateWithDuration:.2 animations:^{
            _shortline.frame = frame;
        }];
        _newlyButton.alpha = 1;
        _hotButton.alpha = .3;
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
